package start.baibu.tool;

import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class FileCheck {


    @Test
    public void t0() throws FileNotFoundException {
        check("file/file1","file/file2",true);
    }

    // 文件1的内容是否包含文件2,true忽略大小写
    public void check(String file1, String file2,boolean ignoreCase) throws FileNotFoundException {
        Scanner input1 =new Scanner(new File(file1));
        Scanner input2 =new Scanner(new File(file2));

        Set<String> set1 = toSet(input1,ignoreCase);
        Set<String> set2 = toSet(input2,ignoreCase);
        Set<String> set3 = new HashSet<>();
        System.out.println("筛查file1包含file2");
        int k = 0;
        for (String s : set2) {
            boolean b = set1.contains(s);
            if (!b) {
                k++;
                set3.add(s);
//                System.out.println("[" + s + "]" + " 不在file1中");
            }
        }
        if(k==0) {
            System.out.println("file1全包含file2");
        } else {
            System.out.println("以下是file2文件多出来的数据["+set3.size()+"]");
            System.out.println(set3);
        }
        k = 0;
        System.out.println("筛查file2包含file1");
        for (String v : set1) {
            boolean b = set2.contains(v);
            if (!b) {
                k++;
                System.out.println("[" + v + "]" + " 不在file2中");
            }
        }
        if(k==0){
            System.out.println("file2全包含file1");
        }
    }

//    public static boolean contains(boolean ignoreCase, Set<String> set, String key){
//
//    }

    public Set<String> toSet(Scanner input, boolean ignoreCase) {
        Set<String> set = new HashSet<>();
        while(input.hasNext()){
            String value=input.next();//遇到分隔符结束这次读取；如\t,\n
            set.add(ignoreCase?value.toLowerCase():value);
        }
        return set;
    }

}
